Choose your input image and save it to Lab1 directory. Choose an image that is different from the ones used as examples in class and choose one that is not the same as your friends. Be creative and have fun!
import cv2 #import OpenCV
from matplotlib import pyplot as plt #import matplotlib
Load an image in grayscale. OpenCV imread takes two parameters: the name of an image file, and a tag 1 for color, 0 for grayscale, and -1 for unchanged (load the image as is). Then we write it to file.
im = cv2.imread('bird.jpg',0)
cv2.imwrite('bird_BW.jpg',im)
Display an image using matplotlib. The cmap argument indicates the colormap. Default is color; ours is grayscale.
plt.imshow(im, cmap='gray')
plt.show()
print(im.shape) #Get the dimension of an image height*width
print(im.dtype) #Get the data type of an image
Our bird image is a 2D array of 900 rows and 900 columns, representing the height and width, respectively. Your image may be of different dimensions and that is okay. Each pixel value is stored in an 8-bit unsigned integer (uint8) for color/intensity scaling in the range of 0 to 255.
In most image processing programming, images are stored in multi-dimensional arrays: 2D for grayscale and 3D for color. Operations on images are thus nothing more than mathematical functions performed on arrays and matrices. In Python, these are supported by the NumPy library. We show a couple examples below.
import numpy as np #import numpy library
print(np.min(im)) #Find the minimum pixel value in an image
print(np.max(im)) #Find the maimum pixel value in an image
First, we reload our bird image. This time, load it in color (using imread with flag=1). Then, display the color image.
im = cv2.imread('bird.jpg',1)
plt.imshow(im)
plt.show()
What has happened to your colorful image? The colors are wrong! OpenCV orders the three color channels as Blue-Green-Red or BGR. However, matplotlib uses different ordering: Red-Greed-Blue or RGB. Since we read an image using OpenCV, our image is BGR. To correctly display the image, we thus have to convert from BGR to RGB before we give it to matplotlib for display.
plt.imshow(cv2.cvtColor(im,cv2.COLOR_BGR2RGB))
plt.show()
Next, let's try to analze the information contained in each of the three channels individually. First, we separate the three color channels into three layers. These channels correspond to the third dimension of our image array. Since our image is BGR, the first layer (index 0) is blue, then green and red. Each layer can be displayed as a 2D grayscale image, whose intensity indicates the amount of the color a pixel contains.
fig, axes = plt.subplots(2, 3, figsize=(30,20))
plt.tight_layout()
axes[0,0].imshow(cv2.cvtColor(im,cv2.COLOR_BGR2RGB))
axes[0,0].set_title("Original", fontsize=32)
axes[0,0].axis('off')
axes[0,1].imshow(im)
axes[0,1].set_title("!!!Incorrect channels!!!", fontsize=32)
axes[0,1].axis('off')
axes[0,2].imshow(cv2.cvtColor(im,cv2.COLOR_BGR2GRAY), cmap='gray')
axes[0,2].set_title("8-Bit Grayscale", fontsize=32)
axes[0,2].axis('off')
axes[1,0].imshow(im[:,:,0], cmap='gray')
axes[1,0].set_title("Blue", fontsize=32)
axes[1,0].axis('off')
axes[1,1].imshow(im[:,:,1], cmap='gray')
axes[1,1].set_title("Green", fontsize=32)
axes[1,1].axis('off')
axes[1,2].imshow(im[:,:,2], cmap='gray')
axes[1,2].set_title("Red", fontsize=32)
axes[1,2].axis('off')
plt.show()
fig.savefig("Lab1Ex1.jpg", bbox_inches='tight')
Observing the results, in the blue layer, the body of both birds are black indicating that there is no blue in these regions. On the other hand, the wings of both birds have a brighter shade of gray because they are blue.
Give the blue channel the blue color. Create a copy of our image. Instead of extracting the blue as we did earlier, we leave it alone. We then set the other two channels to zero.
onecolorB = im.copy()
onecolorB[:,:,1] = 0
onecolorB[:,:,2] = 0